package com.winspread.ui.toast

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.constraintlayout.widget.ConstraintLayout
import com.winspread.ui.R
import java.lang.ref.WeakReference

/**
 *Author: Amin
 *Data: 2021/10/9 14:20
 *
 *Description:
 *
 */
class CustomToast private constructor(context: Context) {
    private var mToast: Toast? = null
    private var mContext: Context = context

    private var sHandler = Handler(Looper.getMainLooper())
    private var sViewWeakReference: WeakReference<View>? = null

    private var gravity = -1
    private var xOffset = -1
    private var yOffset = -1


    companion object {
        private var setToastView: ((View?) -> Unit)? = null

        @Volatile
        var instance: CustomToast? = null

        fun getInstance(context: Context): CustomToast {
            if (instance == null) {
                synchronized(CustomToast::class) {
                    if (instance == null) {
                        instance = CustomToast(context)
                    }
                }
            }
            return instance!!
        }
    }


    class Builder(context: Context) {

        private var toastUtil = getInstance(context)

        /**
         * 设置View
         */
        fun setContentView(@LayoutRes layoutId: Int): Builder {
            val inflateView = toastUtil.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            toastUtil.sViewWeakReference = WeakReference(inflateView.inflate(layoutId, null))
            return this
        }

        fun setToastViewLins(setToastViewLins: ((View?) -> Unit)?): Builder {
            setToastView = setToastViewLins
            return this
        }

        /**
         * 建造者
         */
        fun build(): CustomToast {
            return toastUtil
        }
    }


    /**
     * 安全地显示短时吐司
     *
     * @param text 文本
     */
    fun showShortSafe() {
        sHandler.post { show(Toast.LENGTH_SHORT) }
    }


    /**
     * 显示短时吐司
     *
     * @param text 文本
     */
    fun showShort() {
        show(Toast.LENGTH_SHORT)
    }


    /**
     * 安全地显示长时吐司
     *
     * @param text 文本
     */
    fun showLongSafe() {
        sHandler.post { show(Toast.LENGTH_LONG) }
    }

    /**
     * 显示长时吐司
     *
     * @param text 文本
     */
    fun showLong() {
        show(Toast.LENGTH_LONG)
    }


    private fun show(duration: Int) {
        cancel()
        val toastView = LayoutInflater.from(mContext).inflate(R.layout.toast_base_content_view, null) as ConstraintLayout
        sViewWeakReference?.get()?.let {
            mToast = Toast(mContext)
            setToastView?.run { invoke(it) }
            it.parent?.apply {
                if (this is ViewGroup) {
                    removeView(it)
                }
            }
            toastView.addView(it)
            mToast?.view = toastView
            mToast?.duration = duration
        }
        if (-1 != gravity && -1 != xOffset && -1 != yOffset) {
            mToast?.setGravity(gravity, xOffset, yOffset)
        }
        mToast?.show()
    }


    /**
     * 取消吐司显示
     */
    private fun cancel() {
        mToast?.cancel()
        mToast = null
    }

}